using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

//using SharpMap;
//using SharpMap.CoordinateSystems;
//using SharpMap.CoordinateSystems.Transformations;

namespace IUPUI.StellarResults.Client.UIElements
{
	/// <summary>
	/// A map representational view of <see cref="AstronomicalObject"/>s in a <see cref="Simulation"/> over the earth.
	/// </summary>
	public partial class MapView : UserControl, ISimulationViewTabControl
	{
		#region Private Properties
		private Simulation m_Simulation = null;
		#endregion Private Properties

		#region Public Properties
		/// <summary>
		/// Gets or sets the simulation.
		/// </summary>
		/// <value>The simulation.</value>
		public Simulation Simulation
		{
			get
			{
				return m_Simulation;
			}
			set
			{
				if (m_Simulation != value)
				{
					m_Simulation = value;
					OnSimulationChanged();
				}
			}
		}
		#endregion Public Properties

		#region Constructors
		/// <summary>
		/// Initializes a new instance of the <see cref="MapView"/> class.
		/// </summary>
		public MapView()
		{
			InitializeComponent();

			//// Test code:
			//// mapImage1.Map = CreateMap(new Size(this.Width, this.Height));
			//// mapImage1.Map.ZoomToExtents();

			//SharpMap.Map myMap = new SharpMap.Map(new Size(400, 300));
			//myMap.MinimumZoom = 100; //Minimum zoom allowed
			//myMap.BackColor = Color.White; //Set background
			//myMap.Center = new SharpMap.Geometries.Point(725000, 6180000); //Set center of map
			//myMap.Zoom = 1200; //Set zoom level
			//myMap.Size = new System.Drawing.Size(300, 200); //Set output size

			////Set up the countries layer
			//SharpMap.Layers.VectorLayer layCountries =
			//    new SharpMap.Layers.VectorLayer("Countries");
			//SharpMap.Data.Providers.ShapeFile shpCountries =
			//    new SharpMap.Data.Providers.ShapeFile(@"countries.shp", true);
			//layCountries.DataSource = shpCountries;
			//layCountries.CoordinateTransformation = Transform2Mercator(shpCountries.CoordinateSystem);
			//layCountries.Style.Fill = new SolidBrush(Color.Green);
			//layCountries.Style.Outline = System.Drawing.Pens.Black;
			//layCountries.Style.EnableOutline = true;
			//layCountries.Theme = SetTheme("POPDENS", 0, 400);
			//myMap.Layers.Add(layCountries);
			////Add PostGIS layer:
			////SharpMap.Layers.VectorLayer myLayer = new SharpMap.Layers.VectorLayer("My layer");
			////string ConnStr = "Server=127.0.0.1;Port=5432;UserId=postgres;Password=password;Database=myGisDb;";
			////myLayer.DataSource = new SharpMap.Providers.PostGIS(ConnStr, "myTable", "the_geom", 32632);
			////myLayer.MaxVisible = 40000;
			////myMap.Layers.Add(myLayer);

			//Render the map
			//m_MapPictureBox.Image = CreateMap(m_MapPictureBox.Size).GetMap();
			//m_MapPictureBox.BringToFront();
		}

		/// <summary>
		/// Initializes a new instance of the <see cref="MapView"/> class.
		/// </summary>
		public MapView(
			IContainer container) : this()
		{
			container.Add(this);
		}

		/// <summary>
		/// Initializes a new instance of the <see cref="MapView"/> class.
		/// </summary>
		public MapView(
			IContainer container,
			Simulation simulation)
			: this(container)
		{
			m_Simulation = simulation;
		}
		#endregion Constructors

		/// <summary>
		/// Called when [simulation changed].
		/// </summary>
		protected virtual void OnSimulationChanged()
		{
		}

		#region Test Code
		///// <summary>
		///// Creates the map.
		///// </summary>
		///// <param name="size">The size.</param>
		///// <returns></returns>
		//public static SharpMap.Map CreateMap(
		//    System.Drawing.Size size)
		//{
		//    //Initialize a new map of size 'imagesize'
		//    SharpMap.Map map = new SharpMap.Map(size);
		//    //map.MinimumZoom = 100; //Minimum zoom allowed
		//    //map.BackColor = Color.White; //Set background
		//    //map.Center = new SharpMap.Geometries.Point(725000, 6180000); //Set center of map
		//    //map.Zoom = 1200; //Set zoom level

		//    //Set up the countries layer
		//    SharpMap.Layers.VectorLayer layCountries =
		//        new SharpMap.Layers.VectorLayer("Countries");
		//    SharpMap.Data.Providers.ShapeFile shpCountries =
		//        new SharpMap.Data.Providers.ShapeFile(@"C:\temp\countries.shp", true);
		//    layCountries.DataSource = shpCountries;
		//    //layCountries.CoordinateTransformation = Transform2Mercator(shpCountries.CoordinateSystem);
		//    //layCountries.Style.Fill = new SolidBrush(Color.Green);
		//    //layCountries.Style.Outline = System.Drawing.Pens.Black;
		//    //layCountries.Style.EnableOutline = true;
		//    //layCountries.Theme = SetTheme("POPDENS", 0, 400);
		//    map.Layers.Add(layCountries);

		//    //SharpMap.Layers.LabelLayer lbl = new SharpMap.Layers.LabelLayer("Label");
		//    //lbl.LabelColumn = "POPDENS";
		//    //lbl.Style.Font = new Font(FontFamily.GenericSansSerif, 12, FontStyle.Bold);
		//    //lbl.CoordinateTransformation = layCountries.CoordinateTransformation;
		//    //lbl.DataSource = shpCountries;
		//    //lbl.Style.Halo = new Pen(Color.White, 2);
		//    //lbl.Style.CollisionDetection = true;
		//    //map.Layers.Add(lbl);

		//    return map;
		//}

		///// <summary>
		///// Transforms from a source coordinate system to a mercator transformation
		///// </summary>
		///// <param name="source">The source.</param>
		///// <returns></returns>
		//public static ICoordinateTransformation Transform2Mercator(
		//    ICoordinateSystem source)
		//{
		//    CoordinateSystemFactory cFac = new SharpMap.CoordinateSystems.CoordinateSystemFactory();

		//    System.Collections.ObjectModel.Collection<ProjectionParameter> parameters =
		//        new System.Collections.ObjectModel.Collection<ProjectionParameter>();
		//    parameters.Add(new ProjectionParameter("latitude_of_origin", 0));
		//    parameters.Add(new ProjectionParameter("central_meridian", 0));
		//    parameters.Add(new ProjectionParameter("false_easting", 0));
		//    parameters.Add(new ProjectionParameter("false_northing", 0));
		//    IProjection projection = cFac.CreateProjection("Mercator", "Mercator_2SP", parameters);

		//    IProjectedCoordinateSystem coordsys = cFac.CreateProjectedCoordinateSystem(
		//        "Mercator",
		//        source as IGeographicCoordinateSystem,
		//        projection,
		//        LinearUnit.Metre,
		//        new AxisInfo("East", AxisOrientationEnum.East),
		//        new AxisInfo("North", AxisOrientationEnum.North));

		//    return new CoordinateTransformationFactory().CreateFromCoordinateSystems(source, coordsys);
		//}

		///// <summary>
		///// Sets the theme.
		///// </summary>
		///// <param name="column">The column.</param>
		///// <param name="min">The min.</param>
		///// <param name="max">The max.</param>
		///// <returns></returns>
		//private static SharpMap.Rendering.Thematics.ITheme SetTheme(string column, double min, double max)
		//{
		//    SharpMap.Styles.VectorStyle minstyle = new SharpMap.Styles.VectorStyle();
		//    SharpMap.Styles.VectorStyle maxstyle = new SharpMap.Styles.VectorStyle();

		//    minstyle.Outline = new System.Drawing.Pen(System.Drawing.Color.Gray, 0.5f);
		//    maxstyle.Outline = minstyle.Outline;
		//    minstyle.EnableOutline = true;
		//    maxstyle.EnableOutline = minstyle.EnableOutline;
		//    SharpMap.Rendering.Thematics.GradientTheme theme =
		//        new SharpMap.Rendering.Thematics.GradientTheme(
		//            column, min, max, minstyle, maxstyle);
		//    theme.FillColorBlend = SharpMap.Rendering.Thematics.ColorBlend.Rainbow5;
		//    return theme;
		//}
		#endregion Test Code

		private void m_MapPictureBox_Resize(object sender, EventArgs e)
		{
		}
	}
}
